package cn.yunhe.service.impl;

import cn.yunhe.dao.UserMapper;
import cn.yunhe.eneity.Page;
import cn.yunhe.eneity.User;
import cn.yunhe.service.IUserService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service

public class UserService implements IUserService {


    @Getter
    @Setter
    @Autowired
    private UserMapper userMapper;

    @Override
    public Page queryLikeUsers(Map<String, Object> cond) {
        Page page = new Page();
        //根据前台的条件查询出记录的条数
        page.setTotalNum(userMapper.getLikeUsersCount(cond));
        //从前台数据中获得每页的大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));

        page.setPageSize(pageSize);
        //从前台数据中获得当前页码
        int cPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurpageNum(cPageNum);
        //动态计算出总页数
        page.setTotalPage(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
        //根据条件获得结果列表
        page.setResult(userMapper.getLikeUsers(cond));
        return page;
    }

    @Override
    public void addUser(User user) {

        userMapper.addUser(user);
    }

    @Override
    public void delUser(User user) {
        userMapper.delUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User getUser(User user) {

        return userMapper.getUser(user);
    }
}
